package alipay

import (
	"math"
	"strconv"
)

func (a *Client) F拉起预授权(out_order_no, category, amount, order_title string) {
	/*
		price := free.Price
		bm := make(gopay.BodyMap)
		bm.Set("out_order_no", free.Id).
			Set("out_request_no", free.Id).
			Set("order_title", free.Info).
			Set("amount", price).                //设置押金
			Set("timeout_express", "2d").        //过期时间2天
			Set("notify_url", a.NotifyUrl). //设置返回的url,预授权可以不用返回,前端请求成功后返回再校验
			Set("product_code", "PRE_AUTH_ONLINE")
		//看看后台是否有支付商户id,如果有就加进去,如果要使用花呗必须传

		if app.AliPayeeUserId != "" {
			bm.Set("payee_user_id", app.AliPayeeUserId)
		}
		if app.ZhimaId != "" {
			bm.SetBodyMap("extra_param", func(bm gopay.BodyMap) {
				bm.Set("category", free.RentSort). //商品的分类
									Set("serviceId", app.ZhimaId) //设置芝麻信用的Id

				//租押分离设置
				if free.AssessmentAmount != 0 && app.Assessment >= 20 && app.Assessment <= 80 {
					//设置租押分离参数
					free.AssessmentAmount = util.F租押分离的金额计算(app.Assessment, price)
				}
				if free.AssessmentAmount != 0 {
					bm.SetBodyMap("creditExtInfo", func(bm gopay.BodyMap) {
						bm.Set("assessmentAmount", free.AssessmentAmount)
					})
				}
			})
		}

		if app.Istest == 1 { //如果是测试,把支付金额改成0.01
			bm.Set("amount", 1.2)
		}
	*/
}

func F租押分离的金额计算(ass int8, price float64) (access float64) {
	if ass >= 20 && ass <= 80 {
		//设置租押分离参数
		access = F保留两位小数点(price / 100 * float64(ass))
	}
	return
}

func F保留两位小数点(f float64) float64 {
	f1 := math.Trunc(f*1e2+0.5) * 1e-2
	f1Str := strconv.FormatFloat(f1, 'f', 2, 64)
	value, _ := strconv.ParseFloat(f1Str, 64)
	return value
}
